home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / SWar / Main.p < prev    next >
Text File  |  1995-06-15  |  5KB  |  238 lines

  1. program SWar;
  2.  
  3.     uses
  4. {$IFC UNDEFINED THINK_PASCAL}
  5.         Types, QuickDraw, Events, Menus, Dialogs, Fonts, Resources, Devices, ToolUtils,
  6. {$ENDC}
  7.         Globals, Util, Game, Init;
  8.  
  9.  
  10. {$IFC DEFINED THINK_PASCAL}
  11. PROCEDURE GetMenuItemText(theMenu: MenuRef; item: INTEGER; VAR itemString: Str255);
  12.     INLINE $A946;
  13. {$ENDC}
  14.  
  15.  
  16.     procedure HandleAppleChoice (theItem: Integer);
  17.  
  18.         var
  19.             accName: Str255;
  20.             accNumber: Integer;
  21.             savePort: GrafPtr;
  22.     begin
  23.         case (theItem) of
  24.             1: 
  25.                 ;
  26.             otherwise
  27.                 begin
  28.                     GetPort(savePort);
  29.                     GetMenuItemText(gAppleMenu, theItem, accName);
  30.                     accNumber := OpenDeskAcc(accName);
  31.                     SetPort(savePort);
  32.                 end;
  33.         end; (* case *)
  34.  
  35.     end; (* HandleAppleChoice() *)
  36.  
  37.     procedure HandleFileChoice (theItem: Integer);
  38.         var
  39.             savePort: GrafPtr;
  40.     begin
  41.         case (theItem) of
  42.             1: 
  43.                 begin
  44.                     EnableItem(gFileMenu, 2);
  45.                     EnableItem(gFileMenu, 5);
  46.                     gPlaying := TRUE;
  47.                     gPauseOn := FALSE;
  48.                     InitGame;
  49.                     HideCursor;
  50.                     GetPort(savePort);
  51.                     SetPort(GrafPtr(gPictureWindow));
  52.                     ShowWindow(WindowPtr(gPictureWindow));
  53.             {}
  54.             {        Must set FG and BG pens for CopyBits to work}
  55.             {    }
  56.                     RGBForeColor(myBlack);
  57.                     RGBBackColor(myWhite);
  58.                 end;
  59.             2: 
  60.                 begin
  61.                     if (gPauseOn) then
  62.                         begin
  63.                             gPauseOn := FALSE;
  64.                             HideCursor;
  65.                         end (* if *)
  66.                     else
  67.                         begin
  68.                             gPauseOn := TRUE;
  69.                             ShowCursor;
  70.                         end; (* else *)
  71.                     SetChecks;
  72.                 end;
  73.             3: 
  74.                 begin
  75.                     gSoundOn := not gSoundOn;
  76.                     SetChecks;
  77.                 end;
  78.             5: 
  79.                 ;
  80.             7: 
  81.                 begin
  82.                     gDone := TRUE;
  83.                     EnableItem(gFileMenu, theItem);
  84.                     DisableItem(gFileMenu, 2);
  85.                     DisableItem(gFileMenu, 5);
  86.                 end;
  87.         end; (* end *)
  88.  
  89.     end; (* HandleFileChoice() *)
  90.  
  91.  
  92.     procedure HandleMenuChoice (menuChoice: Longint);
  93.         var
  94.             theMenu: Integer;
  95.             theItem: Integer;
  96.     begin
  97.         if (menuChoice <> 0) then
  98.             begin
  99.                 theMenu := HiWord(menuChoice);
  100.                 theItem := LoWord(menuChoice);
  101.                 case (theMenu) of
  102.                     500: 
  103.                         HandleAppleChoice(theItem);
  104.                     501: 
  105.                         HandleFileChoice(theItem);
  106.                 end; (* case *)
  107.                 HiliteMenu(0);
  108.             end; (* if *)
  109.  
  110.     end; (* HandleMenuChoice() *)
  111.  
  112.     procedure HandleMouseDown;
  113.         var
  114.             whichWindow: WindowPtr;
  115.             thePart: Integer;
  116.             menuChoice, windSize: LongInt;
  117.     begin
  118.         thePart := FindWindow(gTheEvent.where, whichWindow);
  119.         case (thePart) of
  120.             inMenuBar: 
  121.                 begin
  122.                     menuChoice := MenuSelect(gTheEvent.where);
  123.                     HandleMenuChoice(menuChoice);
  124.                 end;
  125.             inSysWindow: 
  126.                 SystemClick(gTheEvent, whichWindow);
  127.             inDrag: 
  128.                 ;
  129. {            DragWindow(whichWindow, gTheEvent.where, &gDragRect);}
  130.             inGoAway: 
  131.                 ;
  132. {            gDone = TRUE;}
  133.             inContent: 
  134.                 ;
  135.             inGrow: 
  136.                 ;
  137.             inZoomIn: 
  138.                 ;
  139.             inZoomOut: 
  140.                 ;
  141.             otherwise
  142.  
  143.         end; (* case *)
  144.  
  145.     end; (* HandleMouseDown *)
  146.  
  147.  
  148.     procedure HandleEvent;
  149.         var
  150.             theChar: char;
  151.             hasEvent: Boolean;
  152.     begin
  153.         if (not wneAvail) then
  154.             begin
  155.                 hasEvent := GetNextEvent(everyEvent, gTheEvent);
  156.                 SystemTask;
  157.             end (* if *)
  158.         else
  159.             hasEvent := WaitNextEvent(everyEvent, gTheEvent, 0, nil);
  160.  
  161.         case (gTheEvent.what) of
  162.             mouseDown: 
  163.                 if ((gPauseOn and gPlaying) or not gPlaying) then
  164.                     HandleMouseDown;
  165.             keyDown, autoKey: 
  166.                 begin
  167.                     theChar := Char(BAnd(gTheEvent.message, charCodeMask));
  168.                     if BAnd(gTheEvent.modifiers, cmdKey) <> 0 then
  169.                         HandleMenuChoice(MenuKey(theChar));
  170.                 end;
  171.             updateEvt: 
  172.                 ;
  173.             nullEvent: 
  174.                 ;
  175.             mouseUp: 
  176.                 ;
  177.             keyUp: 
  178.                 ;
  179.             diskEvt: 
  180.                 ;
  181.             activateEvt: 
  182.                 ;
  183. {Removed some events that will never be interesting.}
  184.             otherwise
  185.                 ;
  186.         end; (* case *)
  187.  
  188.     end; (* HandleEvent() *)
  189.  
  190.  
  191. {Procedure main;}
  192.     var
  193.         tics: Integer;
  194.     const
  195.         _WaitNextEvent = $A860;
  196.         _Unimplemented = $A89F;
  197.         _GetCIcon = $AA1E; {E.g. any Color QuickDraw routine}
  198.         k32bQD = $AB1D;
  199. begin
  200.     MaxApplZone;
  201.     ToolBoxInit;
  202. {$IFC UNDEFINED THINK_PASCAL}
  203.     GetDateTime(LongInt(qd.randSeed));
  204. {$ELSEC}
  205.     GetDateTime(LongInt(randSeed));
  206. {$ENDC}
  207.  
  208.     wneAvail := FALSE;
  209.     if noErr <> SysEnvirons(1, theSysEnv) then
  210.         ;
  211.     if theSysEnv.machineType >= 1 then
  212.         begin
  213.             wneAvail := NGetTrapAddress(_WaitNextEvent, ToolTrap) <> NGetTrapAddress(_Unimplemented, ToolTrap);
  214.             gColorQDFlag := NGetTrapAddress(_GetCIcon, ToolTrap) <> NGetTrapAddress(_Unimplemented, ToolTrap);
  215.             g32bQDFlag := NGetTrapAddress(k32bQD, ToolTrap) <> NGetTrapAddress(_Unimplemented, ToolTrap);
  216.         end; (* if *)
  217.  
  218.     DrawMenuBar;
  219.     WindowInit;
  220.     gDone := FALSE;
  221.     gPlaying := FALSE;
  222.     gPauseOn := FALSE;
  223.     gSoundOn := TRUE;
  224.     gSndIsInitted := FALSE;
  225.     SetChecks;
  226.     LoadPieces;
  227.     LoadSounds;
  228.     InitColors;
  229.     while (not gDone) do
  230.         begin
  231.             if (gPlaying and not gPauseOn) then
  232.                 begin
  233.                     GameCycle;
  234.                 end; (* if *)
  235.             HandleEvent;
  236.         end; (* while *)
  237.     ExitAppl;
  238. end. (* main () *)